package com.leetcode.p200;

import java.util.HashSet;
import java.util.Set;

public class Test219 {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Set<Integer> set = new HashSet<>();

        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                return true;
            }
            set.add(nums[i]);

            if (set.size() > k) {
                set.remove(nums[i - k]);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 1};

        Test219 test = new Test219();
        boolean result = test.containsNearbyDuplicate(arr, 3);
        System.out.println(result);
    }

}
